/**
 * 类工具
 */
package com.cnfangmao.util;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;



public class ClassUtil {
	private final static Logger logger=Logger.getLogger(ClassUtil.class);

	private static Map<Class<?>,Object> mapInstance=new HashMap<Class<?>,Object>();
	
	@SuppressWarnings("unchecked")
	public static synchronized <T> T getInstance(Class<T> clazz,boolean bCreateNew)
	{
		T instance=null;
		
		if(!bCreateNew)
		{
			instance=(T)mapInstance.get(clazz);
		}
		
		if(instance==null)
		{
			try {
				instance=(T)clazz.newInstance();
				mapInstance.put(clazz, instance);
			} catch (Exception e) {
				logger.error(e);
			}
		}
		
		return instance;
	}
	
	@SuppressWarnings({"unchecked","rawtypes"})
	public static synchronized <T> T getInstance(Class<T> clazz,Class<?>[] parameterTypes,Object[] args)
	{
		T instance=null;
		
		if(parameterTypes==null || args==null)
		{
			return instance;
		}
		
		if(instance==null)
		{
			try {
				Constructor con=clazz.getConstructor(parameterTypes);
				instance=(T)con.newInstance(args);
			} catch (Exception e) {
				logger.error(e);
			}
		}
		
		return instance;
	}
		
}
